from google.appengine.ext import webapp

from tools import wiki_manager, category_manager, position_category_manager

class Edit(webapp.RequestHandler):
    '''
    '''

    def post(self):
        '''
        '''
        # retrieve the current language
        language_key = self.request.get('language_key')

        #
        ca_manager = category_manager.CategoryManager(
                language_key=language_key)

        #
        wi_manager = wiki_manager.WikiManager(
                language_key=language_key)

        # retrieve the category key to edit a category
        category_key = self.request.get('category_key')

        # retrieve the page title of the category
        wiki_key = self.request.get('wiki_key')

        #
        if not self.request.get('select') == '':
            #
            category_key = self.request.get('select_category_key')

            # tools save the page
            ca_manager.select(wiki_key, category_key)

            # display the page to edit
            template = wi_manager.edit(wiki_key)
            return self.response.out.write(template)

        elif not self.request.get('new') == '':
            # tools save the page
            category_key = ca_manager.new()

        # display the page to edit
        template = ca_manager.edit(wiki_key, category_key)
        return self.response.out.write(template)

class Save(webapp.RequestHandler):
    '''
    '''

    def post(self):
        '''
        '''
        # retrieve the current language
        language_key = self.request.get('language_key')

        #
        wi_manager = wiki_manager.WikiManager(
                language_key=language_key)

        # retrieve the page
        wiki_key = self.request.get('wiki_key')

        # manage the delete action
        if self.request.get('cancel') == '':
            #
            ca_manager = category_manager.CategoryManager(
                language_key=language_key)

            # retrieve the category key to edit a category
            category_key = self.request.get('category_key')

            if not self.request.get('delete') == '':
                # tools save the page
                ca_manager.delete(category_key)

            elif not self.request.get('new') == '':
                # tools save the page
                category_key = ca_manager.new()

            else:
                # retrieve the category key to edit a category
                title = self.request.get('title')

                # tools save the page
                ca_manager.save(category_key, title)

            # display the page to edit
            template = ca_manager.edit(wiki_key, category_key)
            return self.response.out.write(template)

        else:
            # display the page to edit
            template = wi_manager.edit(wiki_key)
            return self.response.out.write(template)

class Position(webapp.RequestHandler):
    '''
    '''
    def post(self):
        '''
        '''
        # retrieve the current language
        language_key = self.request.get('language_key')

        #
        ca_manager = category_manager.CategoryManager(
                language_key=language_key)

        #
        po_manager = position_category_manager.PositionManager()

        # retrieve the category object from the db
        wiki_key = self.request.get('wiki_key')

        # retrieve the category key
        category_key = self.request.get('category_key')

        # retrieve up action
        up = not self.request.get('up') == ''

        # position update
        po_manager.position(category_key, up)

        # display the page to edit
        template = ca_manager.edit(wiki_key, category_key)
        return self.response.out.write(template)
